package com.app.hanoitips.travel.utils;


import com.app.hanoitips.travel.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

import android.app.Activity;
import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation extends Activity implements LocationListener{
GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.mapview);
	int status=GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
	if(status!=ConnectionResult.SUCCESS){
		int requestCode=10;
		Dialog dialog=GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
		dialog.show();
	}else{
		MapFragment mapFragment=(MapFragment) getFragmentManager().findFragmentById(R.id.fMap);
		googleMap=mapFragment.getMap();
		googleMap.setMyLocationEnabled(true);
		LocationManager locationManager=(LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria=new Criteria();
		String provider=locationManager.getBestProvider(criteria, true);
		Location location=locationManager.getLastKnownLocation(provider);
		if(location!=null){
			onLocationChanged(location);
		}
		
	}
}

@Override
public void onLocationChanged(Location location) {
	// TODO Auto-generated method stub
	double latitude=location.getLatitude();
	double longitude=location.getLongitude();
	LatLng latLng=new LatLng(latitude, longitude);
	googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
	googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
	// TODO Auto-generated method stub
	
}

@Override
public void onProviderEnabled(String provider) {
	// TODO Auto-generated method stub
	
}

@Override
public void onProviderDisabled(String provider) {
	// TODO Auto-generated method stub
	
}
}
